package top.majuan.study.study.servlet.software;

import top.majuan.study.study.dao.TbEmployeeDAO;
import top.majuan.study.study.dao.TbsoftwareDAO;
import top.majuan.study.study.entity.Result;
import top.majuan.study.study.entity.TbEmployee;
import top.majuan.study.study.entity.Tbsoftware;
import top.majuan.study.study.util.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "softwarequery", urlPatterns = "/software/query.action")
public class QueryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        text/plain
        resp.setContentType("application/json");
        PrintWriter writer = resp.getWriter();

        try {
           List<Tbsoftware> list = TbsoftwareDAO.query();
           Result<List<Tbsoftware>> result = Result.success("查询成功", list);
           writer.println(JsonUtils.stringify(result));
        } catch (Exception e) {
            e.printStackTrace();
            Result<String> result = Result.fail("查询发生错误", null);
            writer.println(JsonUtils.stringify(result));
        }

    }
}
